#include <iostream>
#include <memory>
using namespace std;

/*
    unique_ptr:解决了auto_ptr的问题     可以创建多个大小的堆空间

*/

struct student
{
    string name;
    int age;
};

class Cat
{
public:
    Cat(string _name = "" , int _age = 0)
    {
        name = _name;
        age = _age;
    }
    void getAddr(string _name , int _age)
    {
        name = _name;
        age = _age;
    }
    void show()
    {
        cout << "姓名：" << name << "  年龄：" << age << endl;
    }
private:
    string name;
    int age;
};

int main()
{
    // 申请一个int大小的堆空间
    unique_ptr<int> p1(new int);
    *p1 = 78;
    cout << *p1 << endl;

    // 申请十个int大小的堆空间
    unique_ptr<int[]> p2(new int[10]);
    for (int i = 0; i < 10; i++)
    {
        p2[i] = 10 + i;
    }
    for (int i = 0; i < 10; i++)
        cout << p2[i] << " ";
    cout << endl;

    // 申请student大小的堆空间
    unique_ptr<student[]> p3(new student[5]);
    for (int i = 0; i < 5; i++)
    {
        p3[i].name = "张三" + to_string(i);
        p3[i].age = 18 + i;
    }
    for (int i = 0; i < 5; i++)
    {
        cout << "姓名：" << p3[i].name << "  年龄：" << p3[i].age << endl;
    }
    
    cout << "====================================================================" << endl;
    // 申请Cat大小的堆空间
    unique_ptr<Cat[]> p4(new Cat[5]);
    for (int i = 0; i < 5; i++)
    {
        p4[i].getAddr("旺财" + to_string(i), 6 + i);
    }
    for (int i = 0; i < 5; i++)
    {
        p4[i].show();
    }
    

    return 0;
}